package com.example.testproject.adapter

import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.example.testproject.R
import com.example.testproject.bean.TopicFirstBean
import com.example.testproject.bean.TopicSecondBean
import kotlinx.android.synthetic.main.topic_item_second.view.*

/**
 *
 * @ClassName: TopicAdapter
 * @Author: android 超级兵
 * @CreateDate: 10/27/21$ 5:22 PM$
 * TODO
 */
class TopicAdapter(data: List<MultiItemEntity>) :
    BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {
    companion object {
        // 单选
        const val SINGLE_CHOICE = 0

        // 多选
        const val MULTIPLE_CHOICE = 1

        // 判断
        const val JUDGE = 2

        // 填空
        const val FILL = 3

        // 文本
        const val TEXT = 4

        // recyclerView
        const val RECYCLER_VIEW = 5
    }

    init {
        addItemType(TEXT, R.layout.topic_item_first)
        addItemType(RECYCLER_VIEW, R.layout.topic_item_second)
    }

    override fun convert(helper: BaseViewHolder, item: MultiItemEntity) {
        when (helper.itemViewType) {
            TEXT -> {
                item as TopicFirstBean
                helper.setText(R.id.tvFirst, item.title)
            }
            RECYCLER_VIEW -> {
                item as TopicSecondBean
                helper.setText(
                    R.id.tvType,
                    String.format("(%d).%s", item.seq, item.title)
                )

                // 设置布局管理器 如果是判断题改为双布局
                helper.itemView.topicRv.layoutManager =
                    if (item.type == JUDGE) GridLayoutManager(mContext, 2)
                    else LinearLayoutManager(mContext)

                Log.i("szj执行了", "size:${item.child.size}" + item.child.toString())
                val topicContentAdapter = TopicContentAdapter(item.child, item.type)

                topicContentAdapter.click = object : TopicContentAdapter.OnTopicContentListener {
                    override fun singleChoiceClick(type: Int) {
                        // 单选 / 判断
                        if (type == 0 || type == 2) {
                            // 点击单选/判断题时候,将所有选中的数据都变为false
                            topicContentAdapter.data.forEach {
                                it.isSelect = false
                            }
                        }
                    }

                    override fun choiceClick(type: Int) {
                        click.responseClick(type)
                    }

                    override fun fillClick(type: Int) {
                        // 填空回调
                        click.responseClick(type)
                    }
                }

                helper.itemView.topicRv.adapter = topicContentAdapter
            }
        }
    }

    interface OnTopicListener {
        // 响应回调
        fun responseClick(type: Int)
    }

    lateinit var click: OnTopicListener
}